import { createRouter , createWebHashHistory } from "vue-router";

//一级路由
import IndexView from '../views/IndexView.vue'
import ProductView from "../views/ProductView.vue";
import LoginView from "../views/LoginView.vue";
import registerView from "../views/registerView.vue";
import ClassifySmallView from "../views/ClassifySmallView.vue";
import AddressView from "../views/AddressView.vue";
import ConfirmOrderView from "../views/ConfirmOrderView.vue";
import SearchView from "../views/SearchView.vue";
import OrderView from "../views/OrderView.vue";

//子路由
import HomeView from "../views/IndexChild/HomeView.vue";
import ClassifyView from "../views/IndexChild/ClassifyView.vue";
import CartViem from "../views/IndexChild/CartViem.vue";
import UserViem from "../views/IndexChild/UserViem.vue";

let routes = [
    {path: '/', redirect: '/index/home'},
    {path: '/index',
     component: IndexView,
     children:[
        {path: 'home',component: HomeView},
        {path: 'classify',component: ClassifyView},
        {path: 'cart',component: CartViem},
        {path: 'user',component: UserViem},
        {path: 'order',component:OrderView}
    ]
    },
    {path: '/product', component: ProductView},
    {path: '/login' ,component:LoginView},
    {path: '/register',component:registerView},
    {path: '/classifysmall',component:ClassifySmallView},
    {path: '/address',component:AddressView},
    {path: '/confirmorder',component:ConfirmOrderView},
    {path: '/search',component:SearchView}
    

];

//创建路由
let router = createRouter({
    history : createWebHashHistory(),
    routes,
});

//路由拦截
router.beforeEach((to,from,next) =>{
    let TOKEN = window.localStorage.getItem('token');
    if((to.path == '/index/cart' || to.path == '/index/user' || to.path == '/address' || to.path == '/order') && !TOKEN){
		next('/login');
		return;
	};
	next();
})

//导出
export default router